// pages/dnzs/dnzs.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display:'block',
   is_zan:false,
   is_zaned:false,
   is_sea:2,
    uid:'',
	about:[ //标签
		"", "","", "",""
	],
	name:"",		//姓名
	job:"",		//职务
	profession:"",  //公司
	name: "",		//姓名
	show: 0,			//查看数
	love: 0,			//收藏
	sea: 0,  			 	//关注
	sid:2,
  phone: '',
  wxnumber:'',
  email:'',
  address:''
  },
	swichNav:function(e){
		
		this.setData({
			sid: parseInt(e.currentTarget.dataset.current)
		})
	},

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '',
    })
    // console.log(options.id)
    var that=this
    var id = options.id
    this.setData({
      card_id:id
    })
    var uid= wx.getStorageSync('uid')
    if (id == uid){
      this.setData({
        is_my: true
      })
    }else{
      this.setData({
        is_my: false
      })
      if (uid) {
        wx.request({
          url: app.globalData.url + 'xcx/RandarAct',
          data: { act_id: uid, be_act_id: id, act_num: 5 },
          method: "post",
          success: function (res) {
            // console.log(res)
            
          }
        })




    }
    }
   
    wx.request({
      url: app.globalData.url +'xcx/getuser/' + id,
      data:{},
      method:"get",
      success:function(res){

          // console.log(res)
          if (res.data.data){

            if (res.data.data.one){
              var tag = res.data.data.one[0].person_tag
              var tag = tag.split(",");
              if (res.data.data.one[0].is_show_phone == 1) {
                that.setData({
                 
                  is_show_phone: true,

                })
              } else {
                that.setData({
                  
                  is_show_phone: false,

                })
              }

              if (res.data.data.one[0].is_show_wx == 1) {
                that.setData({
                  
                  is_show_wx: true
                })
              } else {
                that.setData({
                 
                  is_show_wx: false
                })
              }

              that.setData({
                card_img: res.data.data.one[0].card_img,
                name: res.data.data.one[0].card_name,
                job: res.data.data.one[0].card_position,
                profession: res.data.data.one[0].card_company,
                show: res.data.data.one[0].visited,
                love: res.data.data.one[0].collect,
                sea: res.data.data.one[0].follower,
                phone: res.data.data.one[0].card_phone,
                wx: res.data.data.one[0].card_wx_number,
                email: res.data.data.one[0].email,
                address: res.data.data.one[0].company_address,
                about: tag,
               

              })
            }
           

          
            if(res.data.data.two){
              var arr = [];
              for (let i = 1; i < res.data.data.two.length; i++) {

                arr[i - 1] = 'https://' + res.data.data.two[i]
              }
              that.setData({
                info: res.data.data.two[0].introduce_info,
                company_name: res.data.data.two[0].all_company_name,
                tax_number: res.data.data.two[0].tax_number,
                bank: res.data.data.two[0].bank,
                bank_account: res.data.data.two[0].bank_account,
                company_url: res.data.data.two[0].company_url,
                imgarr: arr
              })
            }

          }


          if(id==uid){
            wx.setNavigationBarTitle({
              title:  '我的名片'
            })
          }else{
            wx.setNavigationBarTitle({
              title: res.data.data.one[0].card_name + '的名片'
            })
          }
         

      }
    })

    // console.log(that.data.is_my)
    
    wx.request({
      url: app.globalData.url +"xcx/supply/" + id,
      data: {},
      method: "get",
      success: function (res) {
        // console.log(res)
        if (res.data.data){
          that.setData({
            gongarr: res.data.data
          })
        }
        
      }
    })

    wx.request({
      url: app.globalData.url +"xcx/needs/" + id,
      data: {},
      method: "get",
      success: function (res) {
        // console.log(res)
        if(res.data.data){
          that.setData({
            needsarr: res.data.data
          })
        }
       
      }
    })

    var card_id = id//当前名片ID
    
    var wx_id = wx.getStorageSync('uid')//
   
    var that = this
    if(wx_id){
      wx.request({
        url: app.globalData.url +'xcx/Becollected',
        data: { act_id: wx_id, be_act_id: card_id, act_num: '1,2,3,33,11'},
        method:"post",
        success:function(res){
            console.log(res)
            if(res.data.code==10000){
              if (res.data.msg.indexOf("1" != -1) || res.data.msg.indexOf("11" != -1)){
                that.setData({
                  is_zan: true
                })
              }else{
                that.setData({
                  is_zan: false
                })
              }
              if (res.data.msg.indexOf("2") != -1){
                that.setData({
                  is_love: true
                })
              }else{
                that.setData({
                  is_love: false
                })
              }

              if (res.data.msg.indexOf("3") != -1) {
                that.setData({
                  is_sea: 1
                })
              } 
              // else {
              //   that.setData({
              //     is_sea: 2
              //   })
              // }
              if (res.data.msg.indexOf("33") != -1) {
                that.setData({
                  is_sea: 3
                })
              }

            }
        }

       
      })
      
      wx.request({
        url: app.globalData.url+'xcx/NowBecollected',
        data: { act_id: wx_id, be_act_id: card_id, act_num: '1,2,3,33,11' },
        method: "post",
        success:function(res){
          console.log(res)
          wx.hideLoading()
          if(res.data.msg==11){
            that.setData({
              is_zaned: true
            })
          } else {
            that.setData({
              is_zaned: false
            })
          }
          
        }
      })

    }
    

   
  },


  //用户收藏名片的操作
  // love:function(){
  //   var card_id=this.data.card_id//当前名片ID
  //   // console.log(card_id)
  //   var wx_id=wx.getStorageSync('uid')//
  //   // console.log(wx_id)
  //   var that=this
  //   if(wx_id){
  //       wx.request({
  //         url: app.globalData.url +'xcx/RandarAct',
  //         data: { act_id: wx_id, be_act_id: card_id, act_num:2},
  //         method:"post",
  //         success:function(res){
  //           // console.log(res)
  //           if(res.data.code==10000){
  //               that.setData({
  //                   is_love:true
  //               })
  //               wx.showToast({
  //                 title: '收藏成功',
  //                 icon:"success",

  //               })
  //           }
  //         }
  //       })
  //   }

  // },

  sea:function(){
    var card_id = this.data.card_id//当前名片ID
    // console.log(card_id)
    var wx_id = wx.getStorageSync('uid')//
    // console.log(wx_id)
    var that = this
    this.setData({
      display:'none'
    })
    if(wx_id){
      wx.request({
        url: app.globalData.url + 'xcx/RandarAct',
        data: { act_id: wx_id, be_act_id: card_id, act_num: 3 },
        method: "post",
        success:function(res){
          // console.log(res)
          if (res.data.code == 10000) {
            that.setData({
              is_sea: 1
            })
            wx.showToast({
              title: '关注名片成功,请等待对方同意',
              icon: "none",

            })
          }
        }
      })
    }
  },

  tomark:function(){
    var card_id = this.data.card_id//当前名片ID
    // console.log(card_id)
    var wx_id = wx.getStorageSync('uid')//
    wx.navigateTo({
      url: '../mark/mark?act_id=' + wx_id + '&be_act_id=' + card_id,
    })
  },

  cancel:function(e){
    // console.log(e.currentTarget.id)
    var cached_id = e.currentTarget.id
    console.log(cached_id)
    var that=this
    if (cached_id){
      wx.request({
        url: app.globalData.url +'xcx/cancel_follow',
        data: {cached_id:cached_id},
        method:"post",
        success:function(res){
            console.log(res)
            if(res.data.code==10000){
                wx.showToast({
                  title: '取消成功',
                  icon:"none"
                })
                that.setData({
                  is_sea:2
                })
            }
        }
      })
    }
  },
  btn: function () {
    // var that = this
    // wx.addPhoneContact({
    //   firstName: 'test',//联系人姓名  
    //   mobilePhoneNumber: that.data.phone,//联系人手机号  
    //   success:function(res){
    //     console.log(res)
    //   }
    // })  
   var that=this
  //  console.log(12345)
   wx.addPhoneContact({
     firstName: that.data.name, //联系人姓名  
      mobilePhoneNumber: that.data.phone, //联系人手机号 
      weChatNumber: that.data.wx,
      email: that.data.email,
      workAddressStreet: that.data.address,
      success:function(res){
          // console.log(res)
          var card_id=that.data.card_id
          var wx_id=wx.getStorageSync('uid')
          if (res.errMsg == 'addPhoneContact:ok'){
              wx.request({
                url: app.globalData.url +'xcx/RandarAct',
                data: { act_id: wx_id, be_act_id: card_id, act_num: 6 },
                method: "post",
                success:function(res){
                  // console.log(res)
                  if(res.data.code==10000){
                    wx.showToast({
                      title: '保存成功',
                    })
                  }
                }
              })
          }
      }
    })
  },


  // zan: function () {
  //   var card_id = this.data.card_id//当前名片ID
  //   // console.log(card_id)
  //   var wx_id = wx.getStorageSync('uid')//
  //   // console.log(wx_id)
  //   var that = this
  //   if(wx_id){
  //     wx.request({
  //       url: app.globalData.url +'xcx/RandarAct',
  //       data: { act_id: wx_id, be_act_id: card_id, act_num: 1 },
  //       method: "post",
  //       success:function(res){
  //         // console.log(res)
  //         if(res.data.code==10000){
  //           that.setData({
  //               sea:that.data.sea+1,
  //               is_zan: true
  //           })
  //         }
         
  //       }
  //     })
  //   }
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },


  copy: function (e) {
    var that = this;
    var is_show_wx=this.data.is_show_wx
    var is_my = this.data.is_my
    if(is_show_wx==true){
      wx.setClipboardData({
        data: that.data.wx,
        success: function (res) {
          // self.setData({copyTip:true}),  
          wx.showToast({
            title: '复制成功',
            icon: 'success'
          })
        }
      });
    }else{
    if(is_my==true){
      wx.showToast({
        title: '已隐藏微信号',
        icon: 'none'
      })
    }else{
      wx.showToast({
        title: '对方隐藏了微信号',
        icon: 'none'
      })
    }
     
    }
   
  }  
,

  copyemail:function(){
    var that = this;
    wx.setClipboardData({
      data: that.data.email,
      success: function (res) {
        // self.setData({copyTip:true}),  
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var card_id = this.data.card_id//当前名片ID
    // console.log(card_id)
    var wx_id = wx.getStorageSync('uid')//
    // console.log(wx_id)
    var that = this
    if (wx_id){
      wx.request({
        url: app.globalData.url + 'xcx/now_user_collected/' + wx_id + '/' + card_id,
        method: "get",
        success: function (res) {
          console.log(res)
          if (res.data.data) {
            that.setData({
              is_sea: 3,
              cached_id: res.data.data[0].cached_id
            })
          }
        }
      })
    }
    




  },

  map:function(){
    var that=this
    var demo = new QQMapWX({
      key: 'ILVBZ-SBKKF-DDRJ7-NSISQ-UMYTF-VEBJD'
    });  
      //返回可以用于wx.openLocation的经纬度
    demo.geocoder({
     
      address:that.data.address ,
      
          success: function(res) {
       
              console.log(res);
              if (res.message =='query ok'){
                wx.openLocation({
                  latitude: res.result.location.lat,
                  longitude: res.result.location.lng,
                  scale: 18
                })
             }
             
      
        },
    
          fail: function(res) {
       
              console.log(res);
              wx.showToast({
                title: '地址暂时查询不到',
                icon:'none'
              })
        },
     
          

    });



    // var latitude = 28.23489
    // var longitude = 112.94547
    //     wx.openLocation({
    //       latitude: latitude,
    //       longitude: longitude,
    //       scale: 18
    //     })
      
   
  },

  makephone:function(){
    var is_show_phone = this.data.is_show_phone
    var is_my=this.data.is_my
    if (is_show_phone==true){
      wx.makePhoneCall({
        phoneNumber: this.data.phone //仅为示例，并非真实的电话号码
      })
    }else{
      if(is_my==true){
        wx.showToast({
          title: '已隐藏电话',
          icon: 'none'
        })
      }else{
        wx.showToast({
          title: '对方隐藏了电话',
          icon: 'none'
        })
      }

      
    }
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)



    }
   
    var that=this



   
    return {
      title: '您好，我是' + this.data.name+'，这是我的微信名片,请您惠存！',
      path: '',
      imageUrl:this.data.card_img,
      success: function (res) {
        var card_id = that.data.card_id//当前名片ID
        // console.log(card_id)
        var wx_id = wx.getStorageSync('uid')//
        // console.log(wx_id)
        
        if (wx_id) {
          wx.request({
            url: app.globalData.url + 'xcx/RandarAct',
            data: { act_id: wx_id, be_act_id: card_id, act_num: 2 },
            method: "post",
            success: function (res) {
              // console.log(res)
              if (res.data.code == 10000) {
                that.setData({
                  is_love: true,
                  love:that.data.love+1
                })
                wx.showToast({
                  title: '转发成功',
                  icon: "success",

                })
              }
            }
          })
        }
        
      },
      fail: function (res) {
        // 转发失败
        // console.log(res)
        // wx.showToast({
        //   title: '转发失败',
        //   icon: "none",

        // })
      }
    }  
  }
})